/**
 * 数据接口服务端
1. 访问数据验证token
2. token验证失败，携带refresh_token请求用户服务端刷新token
3. 用刷新后的新token重新请求数据接口
 */

const UUID = require("uuid")

const tokenList = []

/**
 * 创建新的 Token 
 */
const createNewToken = (userName) => {
  let uuid = UUID.v4().split("-").join("")
  const time = Date.now()
  const newTokenInfo = {
    token: uuid,
    time,
    userName
  }

  tokenList.push(newTokenInfo)

  return newTokenInfo
}

/**
 * 更新Token
 * @param {*} token 
 * @returns 
 */
const replaceToken = (token) => {
  const tokenInfo = tokenList.find(tokenItem => tokenItem.token === token)
  let uuid = UUID.v4().split("-").join("")
  const time = Date.now()
  tokenInfo.token = uuid
  tokenInfo.time = Date.now()

  return tokenInfo
}

module.exports.createNewToken = createNewToken
module.exports.replaceToken = replaceToken

